package com.enhinck.dbutilweb.common.main;

import com.enhinck.dbutilweb.common.util.FileIterationUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Set;

/**
 * 描述
 *
 * @author huenbin
 * @date 2020-09-18 09:32
 */
@Slf4j
public class FileContentReplaceUtil {
    public static void main(String[] args) {
        Set<String> files = FileIterationUtil.getChildrenFiles("/Users/huenbin/work/newShop", ".gradle");
        log.info("{}", files);

        files.forEach(filePath -> {
            writeFile(filePath, readFileContent(filePath));
        });
    }


    public static String readFileContent(String filePath) {
        BufferedReader br = null;
        String line = null;
        StringBuffer bufAll = new StringBuffer();  //保存修改过后的所有内容，不断增加
        try {
            br = new BufferedReader(new FileReader(filePath));
            while ((line = br.readLine()) != null) {
                if (line.contains("compile(\"com.kingboy:common:1.0\")")) {
                    String newLine = line.replace("compile(\"com.kingboy:common:1.0\")", "compile(\"com.kingboy:common:1.6\")");
                    bufAll.append(newLine);
                } else if (line.contains("compile 'com.greentown.phantom:")) {
                    String newLine = line.replace("compile '", "compile(\"");
                    String[] strs = newLine.split(":");
                    newLine = strs[0] + ":" + strs[1];
                    newLine = newLine + ":${commonVsersion}\")";
                    bufAll.append(newLine);
                } else if (line.contains("springCloudVersion = 'Dalston.SR4'")) {
                    bufAll.append(line);
                    bufAll.append(System.getProperty("line.separator"));
                    bufAll.append("    commonVsersion = '1.1.0'");
                }else if (line.contains("springCloudVersion = 'Edgware.SR6'")) {
                    bufAll.append(line);
                    bufAll.append(System.getProperty("line.separator"));
                    bufAll.append("    commonVsersion = '1.1.0'");
                }
                else {
                    bufAll.append(line);
                }
                bufAll.append("\n");
            }

            // 删除最后一行
            bufAll.deleteCharAt(bufAll.length()-1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    br = null;
                }
            }
        }
        return bufAll.toString();
    }

    //写回文件
    public static void writeFile(String filePath, String content) {
        BufferedWriter bw = null;

        try {
            bw = new BufferedWriter(new FileWriter(filePath));
            bw.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    bw = null;
                }
            }
        }
    }


}
